package com.java1412.controller.api;

import com.java1412.entity.User;
import com.java1412.service.CartService;
import com.java1412.utils.ConstantUtil;
import com.java1412.utils.ResponseUtil;
import com.java1412.vo.CartVo;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author java1412
 * @since 2023-06-25 16:10:34
 */
@RestController
@RequestMapping("/api/cart")
public class CartController {
    @Resource
    private CartService cartService;

    @GetMapping(value = "/findCount")
    public Object findCount(HttpSession session) {
        Object obj = session.getAttribute(ConstantUtil.LOGIN_USER_KEY);
        if (ObjectUtils.isEmpty(obj)) {
            // 没有登录
            return ResponseUtil.fail("用户未登录");
        } else {
            User loginUser = (User) obj;
            Integer userId = loginUser.getId();
            return cartService.findCount(userId);
        }
    }

    @GetMapping(value = "/find")
    public Object find(HttpSession session) {
        Object obj = session.getAttribute(ConstantUtil.LOGIN_USER_KEY);
        if (ObjectUtils.isEmpty(obj)) {
            // 没有登录
            return ResponseUtil.fail("用户未登录");
        } else {
            User loginUser = (User) obj;
            Integer userId = loginUser.getId();
            return cartService.find(userId);
        }
    }

    @PostMapping("/changeCartStatus")
    public Object changeCartStatus(CartVo cartVo) {
        ResponseUtil r = cartService.updateCartStatus(cartVo);
        return r;
    }

    // 根据id删除购物车
    @GetMapping("/deleteCartById/{cartId}")
    public Object deleteCartById(@PathVariable("cartId") Integer cartId) {
        ResponseUtil r = cartService.deleteById(cartId);
        return r;
    }

    // 批量删除
    @GetMapping("/deleteCartsByIds")
    public Object deleteCartsByIds(Integer[] ids) {
        for (Integer id : ids) {
            System.out.println(id);
        }
        ResponseUtil r = cartService.deleteCartsByIds(ids);
        return r;
    }
}
